<template>
	<div class="container last_container">
		<el-table ref="etbl" height="500" v-loading="tableLoading" :data="tableDatas" highlight-current-row @sort-change="sortable">
			<el-table-column property="order_id" label="订单ID" align="center"></el-table-column>
			<el-table-column property="log_type" label="操作类型" align="center"></el-table-column>
			<el-table-column property="current_state" label="当前状态" align="center"></el-table-column>
			<el-table-column property="prev_state" label="上个状态" align="center"></el-table-column>
			<el-table-column property="ip" label="IP" align="center"></el-table-column>
			<el-table-column label="备注" align="center">
				<template slot-scope="scope">
					<div v-html="scope.row.remark || '--'"></div>
				</template>
			</el-table-column>
			<el-table-column property="create_time" sortable="custom" label="创建时间" align="center"></el-table-column>
			<div slot="empty"><el-empty description=" "></el-empty></div>
		</el-table>
		<el-pagination
			background
			:current-page="req.page"
			:page-sizes="[10, 20, 30, 50, 100]"
			:page-size="req.rows"
			layout="total, sizes, prev, pager, next, jumper"
			:total="tableTotal"
			@size-change="sizeChange"
			@current-change="currentChange"
		></el-pagination>
	</div>
</template>

<script>
const __name = 'wmzmall-order-log';
export default {
	data() {
		return {
			dataId: '',
			tableLoading: false,
			req: {
				page: 1,
				rows: 10,
				orderby: 'create_time desc'
			},
			tableDatas: [],
			tableTotal: 0
		};
	},
	methods: {
		async loadData(callback) {
			this.tableLoading = true;
			await this.$db[__name]
				.noaction()
				.noshopid()
				.where({ order_id: this.dataId })
				.totable(this.req)
				.then(res => {
					if (res.code == 200) {
						res.datas.rows.forEach((row, idx) => {
							row.create_time = new Date(row.create_time).format();
						});
						this.tableDatas = res.datas.rows;
						this.tableTotal = res.datas.total;
						
						if (typeof callback === 'function') {
							callback();
						}
					}
					this.tableLoading = false;
				});
		},
		sortable(column) {
			if (column.order) {
				column.order = column.order.replace('ending', '');
			}
			
			this.req.orderby = column.prop + ' ' + (column.order || 'desc');
			
			this.loadData(() => {
				this.$refs.etbl.bodyWrapper.scrollTop = 0;
			});
		},
		sizeChange(size) {
			this.$api.storagePaginationRows(size, this.$route.path + '-log');
			this.req.page = 1;
			this.$api.storagePaginationPage(this.req.page, this.$route.path + '-log');
			this.req.rows = size;
		
			this.loadData(() => {
				this.$refs.etbl.bodyWrapper.scrollTop = 0;
			});
		},
		currentChange(current) {
			this.$api.storagePaginationPage(current, this.$route.path + '-log');
			this.req.page = current;
		
			this.loadData(() => {
				this.$refs.etbl.bodyWrapper.scrollTop = 0;
			});
		}
	},
	created() {
		this.req.page = uni.getStorageSync('__paginationPage' + this.$route.path + '-log') || 1;
		this.req.rows = uni.getStorageSync('__paginationRows' + this.$route.path + '-log') || 10;
		
		this.dataId = this.$route.query.order_id || '';
		this.loadData(() => {
			this.$nextTick(() => {
				this.$api.scrollToStorage('.el-table__body-wrapper', this.$route.path + '-log');
			});
		});
	},
	mounted() {
		this.$nextTick(() => {
			this.$api.scrollOnStorage('.el-table__body-wrapper', this.$route.path + '-log');
		});
	}
};
</script>

<style></style>
